package com.aidex.quartz.domain;

import com.aidex.common.annotation.Excel;
import com.aidex.common.annotation.Excel.ColumnType;
import com.aidex.common.constant.ScheduleConstants;
import com.aidex.common.core.domain.BaseEntity;
import com.aidex.common.utils.StringUtils;
import com.aidex.quartz.util.CronUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.Date;

@Data
@EqualsAndHashCode(callSuper = true)
@ApiModel("定时任务")
public class SysJob extends BaseEntity {

    @ApiModelProperty("任务名称")
    @Excel(name = "任务名称")
    @NotBlank(message = "任务名称不能为空")
    @Size(max = 64, message = "任务名称不能超过64个字符")
    private String jobName;

    @ApiModelProperty("任务组名")
    @Excel(name = "任务组名")
    private String jobGroup;

    @ApiModelProperty("调用目标字符串")
    @Excel(name = "调用目标字符串")
    @NotBlank(message = "调用目标字符串不能为空")
    @Size(max = 500, message = "调用目标字符串长度不能超过500个字符")
    private String invokeTarget;

    /**
     * cron执行表达式
     */
    @ApiModelProperty("Cron表达式")
    @Excel(name = "Cron表达式 ")
    @NotBlank(message = "Cron表达式不能为空")
    @Size(max = 255, message = "Cron表达式不能超过255个字符")
    private String cronExpression;

    @ApiModelProperty("Cron计划策略")
    @Excel(name = "Cron计划策略 ", readConverterExp = "0=默认,1=立即触发执行,2=触发一次执行,3=不触发立即执行")
    private String misfirePolicy = ScheduleConstants.MISFIRE_DEFAULT;

    @ApiModelProperty("是否并发执行")
    @Excel(name = "并发执行", readConverterExp = "0=允许,1=禁止")
    private String concurrent;

    @ApiModelProperty("任务状态")
    @Excel(name = "任务状态", readConverterExp = "0=正常,1=暂停")
    private String status;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date getNextValidTime() {
        if (StringUtils.isNotEmpty(cronExpression)) {
            return CronUtils.getNextExecution(cronExpression);
        }
        return null;
    }
}
